package com.baidumap.administrator.happystudy.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.dialog.loadingDialog.LoadingDialogUtil;
import com.ab.http.OnMessageResponse;
import com.ab.util.AbStrUtil;
import com.ab.util.ToastUtil;
import com.ab.view.simple_rating_bar.SimpleRatingBar;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.model.PingLun;
import com.baidumap.administrator.happystudy.utils.GlideUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 描述：
 * 作者：xiaoxing on 17/4/28 22:30
 * 邮箱：2235445233@qq.com
 */
public class PingJiaActivity extends AppBaseActivity implements OnMessageResponse {
    @Bind(R.id.img_goods_img)
    ImageView imgGoodsImg;
    @Bind(R.id.tv_goods_name)
    TextView tvGoodsName;
    @Bind(R.id.et_content)
    EditText etContent;
    @Bind(R.id.cb_is_ni_ming)
    CheckBox cbIsNiMing;
    @Bind(R.id.simpleRatingBarMiaoShu)
    SimpleRatingBar simpleRatingBarMiaoShu;
    @Bind(R.id.simpleRatingFaHuo)
    SimpleRatingBar simpleRatingFaHuo;
    @Bind(R.id.simpleRatingBarTaiDu)
    SimpleRatingBar simpleRatingBarTaiDu;

    private String mProductName, mProductImg, mProductId, mOrderNo;

    @Override
    public int bindLayout() {
        return R.layout.activity_ping_jia;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        getBundleValue();
    }

    private void getBundleValue() {
        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            mProductName = bundle.getString("product_name");
            mProductImg = bundle.getString("product_img");
            mProductId = bundle.getString("product_id");
            mOrderNo = bundle.getString("order_id");

            GlideUtil.displayImg(this, mProductImg, imgGoodsImg);
            tvGoodsName.setText(mProductName);

        }
    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        setHeaderTitle("我的评价");
    }


    @OnClick({R.id.cb_is_ni_ming, R.id.btn_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cb_is_ni_ming:
                break;
            case R.id.btn_ok:
                if (AbStrUtil.isEmpty(getEidtTextValue(etContent))) {
                    ToastUtil.showMessage(this, "内容不能为空");
                    return;
                }
                String is_anomymous = "";
                if (cbIsNiMing.isChecked()) {
                    is_anomymous = "1";
                } else {
                    is_anomymous = "0";
                }
                LoadingDialogUtil.showGifdialog(getSupportFragmentManager());
                Api.orderComment(this, sHelper.getString(Constant.TOKEN), sHelper.getString(Constant.UID),
                        mOrderNo, mProductId, String.valueOf(simpleRatingBarMiaoShu.getRating()),
                        String.valueOf(simpleRatingFaHuo.getRating()), String.valueOf(simpleRatingBarTaiDu.getRating()), is_anomymous, getEidtTextValue(etContent));
                break;
        }
    }


    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {
        if (url.equals(Constant.ORDER_COMMENT) && jo != null) {
            PingLun m_login = new PingLun(jo.toString());

            if (m_login != null) {


                if (m_login.getCode().equals("200")) {

                    ToastUtil.showMessage(this, "评论成功");
                    setResult(200);
                    finish();

                } else {
                    ToastUtil.showMessage(this, m_login.getMsg());
                }
            }

        }
    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }
}
